package hyy_2022;

import commons.TreeNode;

/**
 * 翻转二叉树
 */
public class _0731_226_InvertTree {


    /**
     * 1、root为空,返回null
     * 2、获取节点的左节点、右节点,递归翻转
     * 3、翻转,node.left = node.right  node.right = node.left
     * @param root
     * @return TreeNode 返回反转后树的根节点
     */
    public static TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }
        TreeNode left = root.left;
        TreeNode right = root.right;
        if(left != null) {
            invertTree(left);
        }
        if(right != null) {
            invertTree(right);
        }
        root.left = right;
        root.right = left;
        return root;
    }
}
